package com.ruoyi.school.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.school.domain.PacLearningFilesData;
import com.ruoyi.school.domain.Student;
import com.ruoyi.school.service.PacLearningFilesDataService;
import com.ruoyi.school.mapper.PacLearningFilesDataMapper;
import com.ruoyi.school.utils.CommonUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jeremylin
 * @description 针对表【pac_learning_files_data(学习档案数据表)】的数据库操作Service实现
 * @createDate 2025-06-05 14:39:45
 */
@Service
public class PacLearningFilesDataServiceImpl extends ServiceImpl<PacLearningFilesDataMapper, PacLearningFilesData>
        implements PacLearningFilesDataService {

    /**
     * 导入数据
     *
     * @param pacLearningFilesData
     */
    @Override
    public void importData(PacLearningFilesData pacLearningFilesData) {
        if (pacLearningFilesData.getStuId() == null) {
            throw new BaseException("导入失败，请选择学生");
        }

        if (pacLearningFilesData.getJsonData() == null) {
            throw new BaseException("导入失败，数据解析失败");
        }

        if (pacLearningFilesData.getLearningType() == null) {
            throw new BaseException("导入失败，请选择学习类型");
        }

        save(pacLearningFilesData);
    }

    /**
     * 查询列表
     *
     * @param pacLearningFilesData
     * @return
     */
    @Override
    public List<PacLearningFilesData> getList(PacLearningFilesData pacLearningFilesData) {
        if (pacLearningFilesData.getStuId() == null) {
            throw new BaseException("学生Id不能为空");
        }

        if (pacLearningFilesData.getLearningType() == null) {
            throw new BaseException("档案类型不能为空");
        }

        List<PacLearningFilesData> list = lambdaQuery().eq(PacLearningFilesData::getStuId, pacLearningFilesData.getStuId())
                .eq(PacLearningFilesData::getLearningType, pacLearningFilesData.getLearningType()).list();
        return list;
    }

    /**
     * 获取6211成绩
     *
     * @param pacLearningFilesData
     * @return
     */
    @Override
    public List<PacLearningFilesData> myList(PacLearningFilesData pacLearningFilesData) {
        Student student = CommonUtils.getStudent();

        if(pacLearningFilesData.getLearningType() == null){
            throw new BaseException("请选择成绩类型");
        }
        LambdaQueryChainWrapper<PacLearningFilesData> queryChainWrapper = lambdaQuery()
                .eq(PacLearningFilesData::getLearningType, pacLearningFilesData.getLearningType())
                .eq(PacLearningFilesData::getStuId, student.getId());

        if (pacLearningFilesData.getYear() != null) {
            queryChainWrapper.apply(
                    "JSON_SEARCH(json_data, 'one', CONCAT({0}, '%'), null, '$[*].学年') IS NOT NULL",
                    pacLearningFilesData.getYear()
            );
        }

        if(pacLearningFilesData.getSemester() != null){
            queryChainWrapper.apply(
                    "JSON_SEARCH(json_data, 'one', {0}, null, '$[*].\"学期\"') IS NOT NULL",
                    pacLearningFilesData.getSemester()
            );
        }


        List<PacLearningFilesData> list = queryChainWrapper.list();

        return list;
    }
}




